package cn.funnymap.lgis.auth;

import java.util.Optional;

/**
 * 用户信息上下文
 *
 * @author jiao xn
 */
public class UserBasicInfoContext {
    private UserBasicInfoContext() {}

    private static final ThreadLocal<UserBasicInfo> THREAD_LOCAL = new ThreadLocal<>();

    public static <T extends UserBasicInfo> void setCurrentUserInfo(T userBasicInfo) {
        THREAD_LOCAL.set(userBasicInfo);
    }

    public static Optional<UserBasicInfo> getCurrentUserBasicInfo() {
        return Optional.ofNullable(THREAD_LOCAL.get());
    }

    public static Optional<String> getCurrentUserId() {
        UserBasicInfo userBasicInfo = getCurrentUserBasicInfo().orElse(new UserBasicInfo());
        return Optional.ofNullable(userBasicInfo.getUserId());
    }

    public static Optional<String> getCurrentUserName() {
        UserBasicInfo userBasicInfo = getCurrentUserBasicInfo().orElse(new UserBasicInfo());
        return Optional.ofNullable(userBasicInfo.getUserName());
    }

    public static Optional<String[]> getCurrentUserRoles() {
        UserBasicInfo userBasicInfo = getCurrentUserBasicInfo().orElse(new UserBasicInfo());
        return Optional.ofNullable(userBasicInfo.getRoles());
    }

    public static void unload() {
        THREAD_LOCAL.remove();
    }
}
